package com.techwells.learningkites.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lidroid.xutils.BitmapUtils;
import com.techwells.learningkites.R;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.model.Course;
import com.techwells.learningkites.networkservice.model.CourseCategory;
import com.techwells.learningkites.networkservice.model.Lesson;
import com.techwells.learningkites.networkservice.model.Word;
import com.techwells.taco.widget.NoScrollGridView;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class CourseExpandListAdapter extends BaseExpandableListAdapter{

	private Context mContext;
	private List<CourseCategory> mList;
	List<List<Course>> mChildList;
	private LayoutInflater mGroupInflater, mChildInflater;
	private SimpleAdapter gridAdapter;
	private List<Map<String, Object>> gridList;
	private BitmapUtils bitmapUtils;


	public CourseExpandListAdapter(Context mContext, List<CourseCategory> mList,
			List<List<Course>> mChildList) {
		super();
		this.mContext = mContext;
		this.mList = mList;
		this.mChildList = mChildList;
		mGroupInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mChildInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		bitmapUtils = new BitmapUtils(mContext);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mChildList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		CellHolderChild cellHolderChild;
		if (convertView == null) {
			cellHolderChild = new CellHolderChild();
			convertView = mGroupInflater.inflate(
					R.layout.item_coursechildlist, null);
			cellHolderChild.img = (ImageView) convertView
					.findViewById(R.id.course_img);
			cellHolderChild.title = (TextView) convertView
					.findViewById(R.id.course_title);
			convertView.setTag(cellHolderChild);

		} else {
			cellHolderChild = (CellHolderChild) convertView.getTag();
		}

		cellHolderChild.title.setText(mChildList.get(groupPosition).get(childPosition).description);
		bitmapUtils.display(cellHolderChild.img, KitesRequestUrl.BASE_URL_IMG + mChildList.get(groupPosition).get(childPosition).icon);

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mChildList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		CellHolderGroup cellHolderGroup;
		if (convertView == null) {
			cellHolderGroup = new CellHolderGroup();
			convertView = mGroupInflater
					.inflate(R.layout.item_courselist, null);
			cellHolderGroup.img = (ImageView) convertView
					.findViewById(R.id.course_img);
			cellHolderGroup.title = (TextView) convertView
					.findViewById(R.id.course_title);

			cellHolderGroup.more = (ImageView) convertView
					.findViewById(R.id.iv_more);
			convertView.setTag(cellHolderGroup);

		} else {
			cellHolderGroup = (CellHolderGroup) convertView.getTag();
		}

		
		cellHolderGroup.title.setText(mList.get(groupPosition).description);
		bitmapUtils.display(cellHolderGroup.img, KitesRequestUrl.BASE_URL_IMG + mList.get(groupPosition).icon);
		
		
		if (isExpanded) {
			cellHolderGroup.more.setImageResource(R.drawable.more_down);
		} else {
			cellHolderGroup.more.setImageResource(R.drawable.more);
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public static class CellHolderGroup implements Serializable {
		TextView title;
		ImageView img, more;
	}

	public static class CellHolderChild implements Serializable {
		public TextView title;
		ImageView img;
	}


}
